package com.ccc.demoboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccc.demoboot.common.R;
import com.ccc.demoboot.pojo.*;
import com.ccc.demoboot.service.MaterialService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Author wz
 * @Date 2024/6/4 10:51
 * @PackageName:com.ccc.demoboot.controller
 * @ClassName: MaterialController
 */
@RequestMapping("/material")
@RestController
public class MaterialController {

    private final MaterialService materialService;

    @Autowired
    public MaterialController(MaterialService materialService) {
        this.materialService = materialService;
    }

    /**
     * 分页查询所有物资
     *
     * @param page
     * @return
     */
    @GetMapping("/select/{page}")
    public R select(@PathVariable("page") int page) {
        PageHelper.startPage(page, 5);//第一个参数表示第几页,第二个参数表示每页的条数
        List<Map<String, Object>> list = this.materialService.selectDuo();
        PageInfo<Map<String, Object>> menuPageInfo = new PageInfo<>(list);
        return new R().add("pageInfo", menuPageInfo);
    }

    /**
     * 查询所有物资
     *
     * @param map
     * @return
     */
    @PostMapping("/selectAllMaterial")
    public R selectAllMaterial(@RequestBody Map<String, Object> map) {
        String material = (String) map.get("material");
        QueryWrapper materialQueryWrapper = new QueryWrapper<>();
        materialQueryWrapper.like("b.materialname", material);
        List<Map<String, Object>> list = materialService.selectAllMaterial(materialQueryWrapper);
        return new R().add("list", list);
    }



    /**
     * 修改用查询
     *
     * @param material
     * @return
     */
    @PostMapping("/upSelectAl")
    public R upSelectAl(@RequestBody Material material) {
        QueryWrapper<Material> materialQueryWrapper = new QueryWrapper<>();
        materialQueryWrapper.setEntity(material);
        List<Material> list = this.materialService.list(materialQueryWrapper);
        return new R().add("list", list);
    }


    /**
     * 物资出库申请根据id查询物资
     *
     * @param material
     * @return
     */
    @PostMapping("/upSelect")
    public R upSelect(@RequestBody Material material) {
        Map<String, Object> one = this.materialService.upSelectId(material.getId());
        return new R().add("one", one);
    }


    /**
     * 根据物资名字查询物资
     *
     * @param code
     * @return
     */
    @PostMapping("/searchSelect")
    public R searchSelect(@RequestBody Code code) {
        System.out.println(code.getMaterialname());
        List<Map<String, Object>> list = this.materialService.searchSelectId(code.getMaterialname());
        return new R().add("list", list);
    }

    /**
     * 修改物资表
     *
     * @param material
     * @return
     */
    @PostMapping("/upMaterial")
    public R upMaterial(@RequestBody Material material) {
        boolean update = this.materialService.updateById(material);
        return new R().add("update", update);

    }

    /**
     * uniAPP查询所有物资
     * @param map
     * @return
     */
    @PostMapping("/uniSelectAllMaterial")
    public R uniSelectAllMaterial(@RequestBody Map<String, Object> map) {
        String material = (String) map.get("material");
        QueryWrapper materialQueryWrapper = new QueryWrapper<>();
        materialQueryWrapper.like("b.materialname", material);
        List<Map<String, Object>> list = materialService.selectAllMaterial(materialQueryWrapper);
        return new R().add("list", list);
    }

    /**
     * uniAPP查询单个物资
     * @param material
     * @return
     */
    @PostMapping("/uniSelectOneMaterial")
    public R uniSelectOneMaterial(@RequestBody Material material) {
        Map<String, Object> one = this.materialService.selectOne(material.getId());
        return new R().add("one", one);
    }

    /**
     * 通过uniAPP查询添加领用的物资
     *
     * @param map 包含物资ID列表的Map对象，其中key为"hobbys"的值是物资ID列表
     * @return 包含查询结果的R对象，其中包含一个名为"list"的键，对应查询到的物资信息列表
     */
    @PostMapping("/uniSelectDuoMaterial")
    public R uniSelectDuoMaterial(@RequestBody Map<String, Object> map) {
        // 从请求体中获取物资ID列表
        List<Object> mapList = (List<Object>) map.get("hobbys");
        // 创建一个列表，用于存储每件物资的信息
        List<Map<String, Object>> list = new ArrayList<>();
        // 遍历物资ID列表
        for (Object o : mapList) {
            // 将ID转换为整型，以便进行数据库查询
            int id = Integer.valueOf(o.toString());
            // 通过物资ID查询物资信息
            Map<String, Object> one = this.materialService.selectOne(id);
            // 将查询到的物资信息添加到列表中
            list.add(one);
        }
        // 创建并返回包含物资信息列表的R对象
        return new R().add("list", list);
    }
}
